package com.jmye.mybatis.mapper;

import com.jmye.mybatis.entity.User;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Description
 * @Date 2024/11/26 下午9:33
 * @Author jmye
 */
public interface UserMapper {

    void addUser(User user);

    @Select("update users set user_name = #{userName}, pass_word = #{passWord} where id = #{id}")
    void updateUser(User user);

    void deleteUser(Integer id);

    @Select("select * from users where id = #{uid}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "pass_word", property = "passWord"),
            @Result(column = "birthday", property = "birthday")
    })
    User findById(Integer id);

    @Select("select * from users")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "user_name", property = "userName"),
            @Result(column = "pass_word", property = "passWord"),
            @Result(column = "birthday", property = "birthday"),
            @Result(column = "id", property = "orderList", javaType = List.class,
                    many = @Many(select = "com.jmye.mybatis.mapper.OrderMapper.findById")
            ),
            @Result(column = "id", property = "roleList", javaType = List.class,
                    many = @Many(select = "com.jmye.mybatis.mapper.RoleMapper.findByUid")
            )
    })
    List<User> findAll();
}
